/*
 * Series.hpp

 *
 *  Created on: 2021��7��26��
 *      Author: root
 */
#pragma  once
#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <iso646.h>
using namespace std;
template<class T>
class Series {
private:
	int m_capacity=100;

	int index = 0;
	bool full = false;
	int size = 0;
	T* p_data;
public:

	Series(int capacity) {
		if (capacity <= 0) {
			capacity = 100;
		}
		this->m_capacity = capacity;
		p_data = new T[capacity];

	}
	Series() {
		p_data = new T[100];
		this->m_capacity = 100;
	}

	~Series() {
		if (this->p_data != nullptr) {
			delete[] p_data;
			p_data=nullptr;
		}
	}

	void clear() {
		this->index = 0;
		this->full = false;
		this->size = 0;
		memset(p_data, 0, sizeof(T) * this->m_capacity);
	}

	int getSize() {
		return this->size;
	}

	T add(T o) {
		(p_data)[this->index++] = o;
		if (!this->full) {
			this->size = this->index;
		}
		if (this->index >= this->m_capacity) {
			this->index = 0;
			this->full = true;
		}
		return o;
	}
	T get(int i) {
		int initiatory = this->index - 1 - (i %= this->m_capacity);
		int actual = initiatory >= 0 ? initiatory : this->m_capacity + initiatory;
		return (T) (p_data)[actual];
	}
	T* getData() {
		return this->p_data;
	}

	bool isFull() {
		return this->full;
	}

	int capacity() {
		return this->capacity;
	}
	Series<T> subSeries(int fromIndex, int length) {
		int start = fromIndex + length;
		Series<T> subSeries(length);
		for (int i = start - 1; i >= fromIndex; --i) {

			subSeries.add(this->get(i));
		}
		return subSeries;
	}

    Series<T>* subSeries(Series<T>* const p_temp,int fromIndex, int length) {
        int start = fromIndex + length;
        for (int i = start - 1; i >= fromIndex; --i) {

            p_temp->add(this->get(i));
        }
        return p_temp;
    }



	T* subData(int fromIndex, int length) {
		int start = fromIndex + length;
		T* p_data=new T[length];
		for (int i = start - 1; i >= fromIndex; --i) {
			p_data[i]=this->get(i);
		}
		return p_data;
	}

};

